const Web3 = require('web3');
// var web3 = new Web3();
// 连接到 ropsten 测试节点
// web3.setProvider(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/6228221f6f894b5684e1b91bba538902"))
const web3 = new Web3(Web3.givenProvider || "http://localhost:7545");
const fs = require('fs')
const source = fs.readFileSync('/Users/binny/blockchainProjects/trufflePro/solidity-note/build/contracts/BNB.json')


let {
    abi,
} = JSON.parse(source)

const BNBAdress = '0xE241ad7625B119C162dddfb04034Fb68DbA8856f'

const privKey =
    "f2cec71fe3ae2df8ec55d11eb1d6c666bfdd5f5f02e7d19b85d990fbcfdbf99c";

const operation = async () => {
    const accounts = await web3.eth.getAccounts()
    console.log(accounts);
    web3.eth.defaultAccount = accounts[0];

    const BNBStorage = new web3.eth.Contract(abi, BNBAdress)
    // console.log(BNBStorage);
    /**
     * 查询账号代币余额
     */
    console.log(accounts[0]);
    let balance = await BNBStorage.methods.balanceOf(accounts[0]).call()
    console.log(balance);


    /**
     * 调用transfer方法:向账户
     */
    // await BNBStorage.methods.transfer(accounts[1], 10000).send({
    //   from: accounts[0]
    // })
    // balance = await BNBStorage.methods.balanceOf(accounts[0]).call()
    // console.log(balance);

    /**
     * approve
     */
    let aa = await BNBStorage.methods.approve(accounts[4], 20000).call()
    console.log(aa);
    // let bb = await BNBStorage.methods.getAllowanceValue(accounts[0]).call()

    console.log(BNBStorage.methods);

    await BNBStorage.methods.transferFrom(accounts[4], accounts[3], 10000).send({
        from: accounts[4],
        gas: 100000
    })
    // balance = await BNBStorage.methods.balanceOf(accounts[3]).call()
    // console.log(balance);



}
operation()